function debounce(callback, wait, immediate) {
  let timeout, result;
  let debounced = function (...args) {
    if(timeout) clearTimeout(timeout)
    // 立即执行
    if (immediate) {
      if (!timeout) result = callback.apply(this, args);
      timeout = setTimeout(() => timeout = null, wait)
      // 不会立即执行
    } else {
      timeout = setTimeout( () => callback.apply(this, args), wait)
    }
    return result
  }
  // 取消执行
  debounced.cancel = function() {
    clearTimeout(timeout)
    timeout = null;
  }
  return debounced
}






// 简单封装
function Debounce1(callback, wait) {
  let timeout;
  return function(...args) {
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout( () => {
      callback.apply(this, args)
      timeout = null;
    },wait )
  }
}